package util

import (
	"fmt"
	"os"
	"path/filepath"
	"runtime"
	"strings"
)

func Debug(formating string, args ...interface{}) {
	log("DEBUG", formating, args...)
}
func Warning(formating string, args ...interface{}) {
	log("WARNING", formating, args...)
}
func Info(formating string, args ...interface{}) {
	log("INFO", formating, args...)
}
func Error(formating string, args ...interface{}) {
	log("ERROR", formating, args...)
}

func log(level string, formating string, args ...interface{}) {
	filename, line, funcname := "???", 0, "???"

	pc, filename, line, ok := runtime.Caller(2)
	if ok {
		funcname = runtime.FuncForPC(pc).Name()      // main.(*MyStruct).foo
		funcname = filepath.Ext(funcname)            // .foo
		funcname = strings.TrimPrefix(funcname, ".") // foo
		filename = filepath.Base(filename)           // /full/path/basename.go => basename.go
	}

	log := fmt.Sprintf("%s: [filename: %s] [line:%d] [function:%s]=>  {%s}  \n", level, filename, line, funcname, fmt.Sprintf(formating, args...))

	switch level {
	case "ERROR":
		fmt.Fprintf(os.Stderr, Red(log))
	case "INFO":
		fmt.Fprintf(os.Stderr, Green(log))
	case "WARNING":
		fmt.Fprintf(os.Stderr, Blue(log))
	case "DEBUG":
		fmt.Fprintf(os.Stderr, Green(log))
	default:
		fmt.Fprintf(os.Stderr, Red(log))
	}

}
